cc.Class({
    extends: cc.Component,

    properties: {
        txtTime: cc.Label,
        txtCoins: cc.RichText,
        txtPhase: cc.Label,
        btnAdd: cc.Node,
        imgHead: cc.Sprite,
        sptHead: cc.SpriteFrame,
        menuCtrl: require('MenuCtrl'),
    },

    onLoad() {
        this.betArea = this.node.find('BetArea', 'BetArea');
        this.betArea.node.on('BET_CLICK', this.onBetClick, this);
        this.betArea.node.on('BET_CANCEL', this.onBetCancel, this);
        this.btnAdd.on('click', this.openShop, this);
        this.menuCtrl.node.find('btn_rank').active = false;
    },

    updateUserInfo() {
        this.updateCoinText();
        cc.loader.load(LINK.userInfo.head, (error, texture) => {
            this.imgHead.spriteFrame = texture ? new cc.SpriteFrame(texture) : this.sptHead;
            this.imgHead.node.width = this.imgHead.node.height = 90;
        });
    },

    updateUserAmount() {
        cc.GameProxy.getUserInfo(data => {
            LINK.userInfo = data;
            this.updateCoinText();
        });
    },

    showBetArea(show) {
        this.menuCtrl.showMenu(show);
        this.betArea.node.x = show ? 0 : 10000;
    },

    onBetClick(evt) {
        var radio = this.betArea.getBetBtn();
        var count = radio.getCount();
        if(LINK.userInfo && LINK.userInfo.amount < count) {
            LinkUtil.onBetNotEnough(() => this.openShop());
            return;
        }
        var motor = evt.detail;
        var name = '';
        if (motor.id == 1){
            name = '哈雷';
        }else if (motor.id == 2){
            name = '宝马';
        }else if (motor.id == 3){
            name = '杜卡迪';
        }else if (motor.id == 4){
            name = '雅马哈';
        }else{
            name = '本田';
        }
        cc.Proxy.newburyingPoint(EventConfig.getEventId(name));
        var bikes = GLOBAL.gameState.bikes;
        cc.Linker(URLS.PlayerBet, {
            amount: count,
            bikeId: motor.motorId,
            gameId: GLOBAL.gameState.gameId
        }).showLoading(false)
        .autoLogin(true)
        .request(data => {
            TdSdk.onUse('摩托', count);
            motor.modify(count);
            radio.flyOil(motor.motorOil.node);
            LINK.userInfo.amount = data;
            this.updateCoinText();
            for(var i in bikes) (bikes[i].id == motor.motorId) && (bikes[i].isBet = true);
        }, msg => {
            (LangLocal.word45 == msg) && cc.Toast.message(LangLocal.word46);
        });
    },

    onBetCancel() {
        cc.Linker(URLS.CancelBet, {
            gameId: GLOBAL.gameState.gameId
        }).autoLogin(true).showLoading(false)
        .request(data => {
            this.betArea.clearBet();
            LINK.userInfo.amount = data;
            this.updateCoinText();
            var bikes = GLOBAL.gameState.bikes;
            for(var i in bikes) bikes[i].isBet = false;
        });
    },

    onPlayerBet(timer) {
        if(!this.betArea.motors) {
            this.betArea.loadMotors();
        }
        this.updatePhase();
        this.updateTime(timer);
        this.showBetArea(true);
        this.betArea.updateBetData();
        this.betArea.clearBet();

        this.scheduleOnce(() => {
            if(timer > 5 && GLOBAL.IS_NEW_PLAYER) {
                cc.Toast.message(LangLocal.word44);
                GLOBAL.IS_NEW_PLAYER = false;
                this.betArea.showGuide();
            }
        }, 1);
    },

    onMoveFinish(timer) {
        if(LINK.userInfo) {
            TdSdk.onReward(GLOBAL.playerResult.currentAmount-LINK.userInfo.amount, '摩托');
            LINK.userInfo.amount = GLOBAL.playerResult.currentAmount;
            this.updateCoinText();
        }
        cc.Popup.show('ResultPop', pop => {
            cc.Sound.pauseMusic('snd_run');
            cc.Sound.playMusic('snd_bg');
            pop.delayClose(timer + 1);
        }, () => this.node.emit('MOVE_RESTART'));
    },

    openShop() {
        cc.Proxy.newburyingPoint(EventConfig.getEventId('充值'));
        cc.GameProxy.openShop(pop => pop.node.parent.setScale(0.66));
    },

    updateCoinText() {
        this.txtCoins.string = '<outline color=black width=1><b>' + cc.Global.formatNumber(LINK.userInfo.amount) + '</b></outline>';
        var width = this.txtCoins.node.width;
        if(width > 130) this.txtCoins.node.setScale(130 / width);
        else this.txtCoins.node.setScale(1);
    },

    updatePhase() {
        var phase = GLOBAL.gameState.phase;
        this.txtPhase.string = phase.substr(9, phase.length);
    },

    updateTime(t) {
        this.txtTime.string = t < 10 ? ('0' + t) : t;
        var node = this.txtTime.node;
        node.setScale(0.6);
        node.runAction(cc.scaleTo(0.3, 1).easing(cc.easeBackOut()));
    },

    onTiktok(t) {
        if(t > 0) {
            this.updateTime(t - 1);
        } else {
            this.node.emit('MOVE_READY');
        }
    },
});
